SAPIEN.action.EditRoleAction = (function () {
	var acls = new SAPIEN.data.Collection();
	var categories =  new SAPIEN.data.Collection();
	return {
		initTabs : function () {
			$('#tabs').tabs();
		},
		initControls : function () {
			var roleId = $('#update_role_id').val();
			var aclUrl = $('#category_acls_json_url').val();
			var categoryUrl = $('#category_json_url').val();
			$.getJSON(categoryUrl, function(jsonCategories) {
				var categoryControl = $('#create_category_acl_categoryId').html('');
				$.each(jsonCategories, function() {
					categories.add(this);
					categoryControl.append('<option value="' + this.id + '">' + this.name + '</option>');
				});
				$.getJSON(aclUrl, {roleId:roleId}, function(jsonAcls) {
					var aclControl = $('#category_acls').html('');
					$.each(jsonAcls, function() {
						acls.add(this);
						var category = categories.get(this.categoryId);
						aclControl.append('<option value="' + this.id + '">' + category.name + '</option>');
						$('#create_category_acl_categoryId option[value=' + category.id + ']').remove();
					});
					if ($('#create_category_acl_categoryId option').size() == 0) {
						$('#category_acls_add').attr('disabled','disabled');
					}
					if ($('#category_acls option').size() == 0) {
						$('#category_acls_remove').attr('disabled','disabled');
						$('#update_category_acl_save').attr('disabled','disabled');
					}
				});
			});
		},
		initDialogs : function () {
			$('#add_category').dialog({
				width: 250,
				autoOpen: false,
				resizable: false,
				modal: true, 
				buttons: {
					'Add':function () {
						SAPIEN.action.EditRoleAction.addPermission();
					},
					'Cancel': function() {
						$(this).dialog('close');
					}
				}
			});
			$('#set_permissions').dialog({
				width: 265,
				autoOpen: false,
				resizable: false,
				modal: true, 
				buttons: {
					'Save':function () {
						SAPIEN.action.EditRoleAction.updatePermission();
						$(this).dialog('close');
					},
					'Close': function() {
						$(this).dialog('close');
					}
				}
			});
		},
		initEvents : function () {
			// Add click event to categories add button
			$('#category_acls_add').click(function() { $('#add_category').dialog('open'); });
			// Add change event to categories listing.
			$('#category_acls').click(function() {
				console.log('title = ' + $('#category_acls :selected').text());
				var acl = acls.get($(this).val());
				SAPIEN.data.setProperties(acl, 'update_category_acl');
				$('#update_category_acl_save').removeAttr('disabled');
				$('#set_permissions').dialog('option', 'title', $('#category_acls :selected').text());
				$('#set_permissions').dialog('open');
			});
			$('#category_acls_remove').click(function(){
				SAPIEN.action.EditRoleAction.removePermission();
			});
			$('#update_category_acl_save').click(function() {
				SAPIEN.action.EditRoleAction.updatePermission();
			})
		},
		init : function () {
			this.initTabs();
			this.initControls();
			this.initDialogs();
			this.initEvents();
		},
		addPermission : function () {
			var form = $('#create_category_acl');
			$.post(form.attr('action'), form.serialize(), function (data) {
				var json = eval( "(" + data + ")" );
				if (typeof json === "object") {
					$('#category_acls').append('<option value="' + json.acl.id + '">' + json.category.name + '</option>');
					$('#create_category_acl_categoryId option[value=' + json.category.id + ']').remove();
					acls.add(json.acl);
				}
				if ($('#create_category_acl_categoryId option').size() == 0) {
					$('#category_acls_add').attr('disabled','disabled');
				}
				$('#category_acls_remove').removeAttr('disabled');
				$('#add_category').dialog('close');
			});
		},
		removePermission : function () {
			var action = $('#remove_category_acl_url').val();
			var aclControl = $('#category_acls');
			var aclId = aclControl.val();
			var categoryControl = $('#create_category_acl_categoryId');
			$.post(action, {'id':aclId}, function (data) {
				var json = eval( "(" + data + ")" );
				if (typeof json === "object" && json.success === true) {
					var acl = acls.get(aclId);
					var category = categories.get(acl.categoryId);
					categoryControl.append('<option value="' + category.id + '">' + category.name + '</option>');
					$('#category_acls option[value=' + aclId + ']').remove();
					acls.remove(aclId);
					$('#category_acls_add').removeAttr('disabled');
					if ($('#category_acls option').size() == 0) {
						$('#category_acls_remove').attr('disabled','disabled');
						$('#update_category_acl_save').attr('disabled','disabled');
					}
				}
			});
		},
		updatePermission : function () {
			var form = $('#update_category_acl');
			$.post(form.attr('action'), form.serialize(), function (data) {
				var json = eval( "(" + data + ")" );
				if (typeof json === "object") {
					acls.add(json.acl);
				}
			});
		},
		test : function() {
			console.log('Output Category IDs');
			categories.test();
			console.log('Output ACL IDs');
			acls.test();
		}
	};
})();
$(document).ready(function () {
	SAPIEN.action.EditRoleAction.init();
});
	